import { Component, OnInit } from '@angular/core';

// 导入路由模块
import { Router,ActivatedRoute } from '@angular/router';

// 导入storage本地存储服务
import { StorageService } from '../../service/storage.service';

@Component({
  selector: 'app-contact-edit',
  templateUrl: './contact-edit.component.html',
  styleUrls: ['./contact-edit.component.css']
})
export class ContactEditComponent implements OnInit {

  private id:number;
  private contact:{id:number,name:string,tel:string,email:string,tagId:number};
  private tagList:{id:number,name:string}[];
  constructor(
    private router: Router,
    private route:ActivatedRoute,
    private storage:StorageService
  ) { }

  ngOnInit() {
    // console.log('ngOnInit执行了');
    // this.route.queryParams.subscribe((params: any) => {
    //   console.log(params);
    // });


    // 通过参数订阅
    // this.route.params.subscribe((params:any)=>{
    //   console.log(params);
    //   this.id=params.id;
    // });

    // 直接获取
    this.id=this.route.snapshot.params.id;
    this.contact=this.storage.contactList.find(item=>item.id==this.id);
    this.tagList=this.storage.tagList;
    
  }

  updateHandle(f){
    if(!f.valid){
      return alert('请填写完整的表单数据')
    }
    console.log(this.contact);
    this.storage.contactEdit(this.contact);
    alert('修改成功');
    this.router.navigate(['/contactlist']);
  }

}
